import { request } from '@/utils/request'

enum API {
  SPU_INFO_URL = '/api/product/spuinfo/list',
  SPU_UP_URL = '/api/product/spuinfo',
  BRAND_LIST_URL = '/api/product/categorybrandrelation/brands/list',
  SPU_BASE_URL = '/api/product/attrgroup',
  SPU_ATTRS_URL = '/api/product/attr/sale/list',
  SPU_ATTRS_SAVE = '/api/product/spuinfo/save',
  SKU_INFO_LIST = '/api/product/skuinfo/list',
}

export const reqSpuInfo = (params: any) => {
  // 过滤掉值为0的catelogId、brandId、min、max，以及key为空字符串的情况
  const filteredParams = Object.fromEntries(
    Object.entries(params).filter(([key, value]) => {
      if (['catelogId', 'brandId', 'min', 'max'].includes(key))
        return value !== 0
      if (key === 'key') return value !== ''
      return true
    }),
  )
  return request.get<any, any>(API.SPU_INFO_URL, {
    params: Object.keys(filteredParams).length ? filteredParams : undefined,
  })
}

export const reqBrandList = (catId: any) =>
  request.get<any, any>(API.BRAND_LIST_URL + `?catId=${catId}`)

export const reqUpSpu = (id: number) =>
  request.post<any, any>(`${API.SPU_UP_URL}/${id}/up`)

export const reqBaseList = (catalogId: number) =>
  request.get<any, any>(API.SPU_BASE_URL + `/${catalogId}` + '/withattr')

export const reqAttrsList = (catalogId: number, limit: number, page: number) =>
  request.get<any, any>(
    API.SPU_ATTRS_URL + `/${catalogId}?limit=${limit}&page=${page}`,
  )

export const reqSaveSpu = (spu: any) =>
  request.post<any, any>(API.SPU_ATTRS_SAVE, spu)

export const reqSkuInfoList = (
  catelogId: number,
  brandId: number,
  page: number,
  limit: number,
  key: string,
  min: number,
  max: number,
) =>
  request.get<any, any>(
    API.SKU_INFO_LIST +
      `?catelogId=${catelogId}&brandId=${brandId}&page=${page}&limit=${limit}&min=${min}&max=${max}&key=${key}`,
  )
